﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Liang.DataBase.Model;
using Liang.UI;
using Liang.UI.Tools;
using WeifenLuo.WinFormsUI.Docking;
using Liang.DataBase.Tools;
using Liang.DataBase.BLL;
using Liang.DataBase.Model.Entity;
using Liang.Model;

namespace Client
{
    public partial class FrmApp : DockContent
    {
        private bool query = false;

        public FrmApp()
        {
            InitializeComponent();           
        }

        /// <summary>
        /// 初始化dataGridView
        /// </summary>
        public void initView()
        {           
            dataView.AutoGenerateColumns = false;
            dataView.Columns.Add(FormHelper.createCheckBoxColumn());
            dataView.Columns.Add(FormHelper.createTextColumn("appId", "appId", 50, false));
            dataView.Columns.Add(FormHelper.createTextColumn("功能名称", "name", 200, true));
            dataView.Columns.Add(FormHelper.createTextColumn("功能级别", "basicdata.des", 200, true));
            //dataView.Columns.Add(FormHelper.createTextColumn("所属功能组合", "module.name", 200, true));
            //追加
            dataView.Columns.Insert(dataView.Columns.Count, FormHelper.createLinkColumn("Detail", "编辑", true));

            pager.Bind();
        }

        /// <summary>
        /// 显示编辑页面
        /// </summary>
        public void showEdit(sys_app app = null)
        {
            FrmAppEdit formEdit = new FrmAppEdit(app);

            if (DialogResult.OK == formEdit.ShowDialog())
            {
                pager.Bind();
            }
        }

        /// <summary>
        /// 查询
        /// </summary>
        public int queryBind()
        {
            // 分页设置
            PageInfo pagerInfo = new PageInfo
            {
                CurrentPageIndex = pager.PageCurrent,
                PageSize = pager.PageSize,
                RecordCount = pager.PageCurrent * pager.PageSize
            };

            // 查询条件
            var where = LambdaHelper.True<sys_app>();
            if (query)
            {
                //whereStr = QueryHelper.GetWhereString(this.groupBox1);
            }

            AppBLL appBLL = new AppBLL();
            IList<sys_app> list = appBLL.FindWithPagerAndOrderby(where, ref pagerInfo, "createTime");

            //绑定分页控件   
            pager.bindingSource.DataSource = list;
            pager.bindingNavigator.BindingSource = pager.bindingSource;
            //分页控件绑定DataGridView   
            dataView.DataSource = pager.bindingSource;
            //返回总记录数   
            return pagerInfo.RecordCount;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            showEdit();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            query = true;
            pager.Bind();
        }

        private void btnQueryAll_Click(object sender, EventArgs e)
        {
            query = false;
            pager.Bind();
        }

        private void dataView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dataView.Columns[e.ColumnIndex].Name == "Detail" && 0 <= e.RowIndex)
            {
                Guid strAppId = (Guid)dataView.Rows[e.RowIndex].Cells["appId"].Value;
                AppBLL appBLL = new AppBLL();
                sys_app app = appBLL.FindByID(strAppId);

                if (null == app)
                {
                    MessageBox.Show("未知错误");
                }
                else
                {
                    showEdit(app);
                }                
            }
        }

        private void FrmModuleApp_Load(object sender, EventArgs e)
        {
            initView();
        }

        private int pager_EventPaging()
        {
            return queryBind();
        }

        private EventPagingArg pager_EventExcel()
        {
            return new EventPagingArg("程序功能", this.dataView);
        }

        private EventPagingArg pager_EventDelItem()
        {
            return new EventPagingArg("删除功能", this.dataView);
        }
    }
}
